using System;
using CommunityServer.Components;
using System.Collections;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls
{
	/// <summary>
	/// Summary description for InlineTagEditor.
	/// </summary>
	public class InlineTagEditor : CommunityServer.Controls.InlineTagEditor
	{
		CSContext csContext = CSContext.Current;

		public override void SaveTags(int postID, string[] tags)
		{
			if (this.IsEditable(postID))
			{
				ForumPost post = Posts.GetPost(postID, csContext.User.UserID);
				post.Categories = tags;
				Posts.UpdatePost(post, csContext.User.UserID);
			}
		}

		public override string GetUrl(string tag)
		{
			return SiteUrls.Instance().TagsBrowser(new string[] { tag });
		}

		public override bool IsEditable (int postID)
		{
			return IsEditable();
		}

		public override bool IsEditable()
		{
			return Permissions.ValidatePermissions(this.Forum, Permission.Moderate | Permission.Edit, csContext.User);
		}

		Forum _forum = null;
		public Forum Forum
		{
			get
			{
				if (_forum == null)
				{
					if (csContext.SectionID > 0)
						_forum = Forums.GetForum(csContext.SectionID);
					else if (csContext.PostID > 0)
						_forum = Forums.GetForumByPostID(csContext.PostID, csContext.User.UserID, true);
				}

				return _forum;
			}
			set
			{
				_forum = value;
				_availableTags = null;
			}
		}

		string[] _availableTags = null;
		public override string[] AvailableTags 
		{ 
			get
			{
				if (_availableTags == null)
				{
					ArrayList allTags = PostCategories.GetCategories(this.Forum.SectionID, true, false);
					ArrayList availableTags = new ArrayList();
					foreach (PostCategory pc in allTags)
					{
						availableTags.Add(pc.Name);
					}

					_availableTags = (string[]) availableTags.ToArray(typeof(string));
				}

				return _availableTags;
			}
		}
	}
}
